/*
 *  Copyright 2008-2010 the original author or authors.
 * 
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 * 
 *       http://www.apache.org/licenses/LICENSE-2.0
 * 
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *  under the License.
 */
package org.openaecfm.spf.io.utility;

import org.openaecfm.model.actor.PersonAndOrganization;
import org.openaecfm.model.utility.Application;
import org.openaecfm.model.utility.ChangeActionEnum;
import org.openaecfm.model.utility.OwnerHistory;
import org.openaecfm.model.utility.StateEnum;
import org.openaecfm.spf.io.AbstractEntityInstanceInputImpl;
import org.openaecfm.spf.io.IAppDataManager;
import org.openaecfm.spf.io.IEntityInstanceOutput;
import org.openaecfm.spf.model.DollarParameter;
import org.openaecfm.spf.model.EntityInstance;
import org.openaecfm.spf.model.EntityInstanceNameParameter;
import org.openaecfm.spf.model.EnumerationParameter;
import org.openaecfm.spf.model.IntegerParameter;
import org.openaecfm.spf.model.NodeType;
import org.openaecfm.spf.model.Parameter;
import org.openaecfm.spf.model.SimpleEntityInstance;

public final class OwnerHistoryIO extends
		AbstractEntityInstanceInputImpl<OwnerHistory> implements
		IEntityInstanceOutput<OwnerHistory> {
	/**
	 * {@inheritDoc}
	 */
	@Override
	public String exportSPF(OwnerHistory pojo, IAppDataManager appDataManager) {
		SimpleEntityInstance ei = new SimpleEntityInstance("IFCOWNERHISTORY",
				appDataManager.getName(pojo));

		// Set OwningUser
		EntityInstanceNameParameter ouser = new EntityInstanceNameParameter(
				appDataManager.getName(pojo.getOwningUser()));
		ei.addParameter(ouser);

		// Set OwningApplication
		EntityInstanceNameParameter oapp = new EntityInstanceNameParameter(
				appDataManager.getName(pojo.getOwningApplication()));
		ei.addParameter(oapp);

		// Set State
		if (pojo.getState() == null) {
			DollarParameter d = DollarParameter.getInstance();
			ei.addParameter(d);
		} else {
			EnumerationParameter e = new EnumerationParameter(pojo.getState());
			ei.addParameter(e);
		}

		// Set ChangeAction
		EnumerationParameter changeaction = new EnumerationParameter(
				pojo.getChangeAction());
		ei.addParameter(changeaction);

		// Set LastModifiedDate
		if (pojo.getLastModifiedDate() == null) {
			DollarParameter d = DollarParameter.getInstance();
			ei.addParameter(d);
		} else {
			IntegerParameter i = new IntegerParameter(
					pojo.getLastModifiedDate());
			ei.addParameter(i);
		}

		// Set LastModifyingUser
		if (pojo.getLastModifyingUser() == null) {
			DollarParameter d = DollarParameter.getInstance();
			ei.addParameter(d);
		} else {
			EntityInstanceNameParameter n = new EntityInstanceNameParameter(
					appDataManager.getName(pojo.getLastModifyingUser()));
			ei.addParameter(n);
		}

		// Set LastModifyingApplication
		if (pojo.getLastModifyingApplication() == null) {
			DollarParameter d = DollarParameter.getInstance();
			ei.addParameter(d);
		} else {
			EntityInstanceNameParameter n = new EntityInstanceNameParameter(
					appDataManager.getName(pojo.getLastModifyingApplication()));
			ei.addParameter(n);
		}

		// Set CreationDate
		IntegerParameter cdate = new IntegerParameter(pojo.getCreationDate());
		ei.addParameter(cdate);

		return ei.toString();
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	protected OwnerHistory doCreateObject() {
		return new OwnerHistory();
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public void resolveReferenceParameter(EntityInstance ei, OwnerHistory pojo,
			IAppDataManager appDataManager) {
		SimpleEntityInstance sei = (SimpleEntityInstance) ei;

		// Set OwningUser
		EntityInstanceNameParameter ouser = (EntityInstanceNameParameter) sei
				.getParameter(0);
		pojo.setOwningUser((PersonAndOrganization) appDataManager
				.getObject(ouser.toInstanceName()));

		// Set OwningApplication
		EntityInstanceNameParameter oapp = (EntityInstanceNameParameter) sei
				.getParameter(1);
		pojo.setOwningApplication((Application) appDataManager.getObject(oapp
				.toInstanceName()));

		// Set LastModifyingUser
		Parameter lmu = sei.getParameter(5);
		if (lmu.getNodeType() == NodeType.ENTITY_INSTANCE_NAME) {
			EntityInstanceNameParameter n = (EntityInstanceNameParameter) lmu;
			pojo.setLastModifyingUser((PersonAndOrganization) appDataManager
					.getObject(n.toInstanceName()));
		}

		// Set LastModifyingApplication
		Parameter lma = sei.getParameter(6);
		if (lma.getNodeType() == NodeType.ENTITY_INSTANCE_NAME) {
			EntityInstanceNameParameter n = (EntityInstanceNameParameter) lma;
			pojo.setLastModifyingApplication((Application) appDataManager
					.getObject(n.toInstanceName()));
		}
	}

	@Override
	protected void resolveNotReferenceParameter(EntityInstance ei,
			OwnerHistory pojo) {
		SimpleEntityInstance sei = (SimpleEntityInstance) ei;

		// Set State
		Parameter state = sei.getParameter(2);
		if (state.getNodeType() == NodeType.ENUMERATION) {
			EnumerationParameter e = (EnumerationParameter) state;
			pojo.setState(StateEnum.valueOf(e.toEnumString()));
		}

		// Set ChangeAction
		EnumerationParameter ca = (EnumerationParameter) sei.getParameter(3);
		pojo.setChangeAction(ChangeActionEnum.valueOf(ca.toEnumString()));

		// Set LastModifiedDate
		Parameter lmd = sei.getParameter(4);
		if (lmd.getNodeType() == NodeType.INTEGER) {
			IntegerParameter i = (IntegerParameter) lmd;
			pojo.setLastModifiedDate(i.toInteger());
		}

		// Set CreationDate
		IntegerParameter cd = (IntegerParameter) sei.getParameter(7);
		pojo.setCreationDate(cd.toInteger());
	}
}
